
******************** DISCRETE CHOICE EXPERIMENT 1 (Projects) *************************

// Reshape Data for Analysis
/*
Right now, the data is in wide shape. To run the regression, we need the data
in long format, i.e we need to have only one column with a "choice" dummy.
The question, which asks them to make a choice, will also be in one column.
So the same respondent id will be repeated for 12 lines (6 questions that
asks them to make choices, two choice options in each)
*/

	use "$root/output/analysis_finaldataset.dta", clear
	
	// Keep Relevant Data

	snapshot erase _all
	snapshot save, label(original)
	
	keep country_id agencyid minister subnational responseid male region current_emp experience lowexp ///
		 dce?_r? rd_?_?_budget rd_?_?_project rd_?_?_technicalassistance ///
		  kn_learning googlelist
		  
		 gen agency2=agencyid==11
		 replace agency2=. if agencyid==.

	// Rename

	**ROUND 1, BUNDLE CHOICES A AND B should end with 1 and 2 for each category
		rename (rd_1_a_project rd_1_b_project rd_1_a_technicalassistance rd_1_b_technicalassistance rd_1_a_budget rd_1_b_budget) ///
			   (rd1_project1 rd1_project2  rd1_technicalassistance1 rd1_technicalassistance2 rd1_budget1 rd1_budget2)

	**ROUND 2, BUNDLE CHOICES A AND B should end with 3 and 4 for each category
		rename (rd_2_a_project rd_2_b_project rd_2_a_technicalassistance rd_2_b_technicalassistance rd_2_a_budget rd_2_b_budget) ///
			   (rd1_project3 rd1_project4  rd1_technicalassistance3 rd1_technicalassistance4 rd1_budget3 rd1_budget4)

	**ROUND 3, BUNDLE CHOICES A AND B should end with 5 and 6 for each category	
		rename (rd_3_a_project rd_3_b_project rd_3_a_technicalassistance rd_3_b_technicalassistance rd_3_a_budget rd_3_b_budget) ///
			   (rd1_project5 rd1_project6  rd1_technicalassistance5 rd1_technicalassistance6 rd1_budget5 rd1_budget6)

	**ROUND 4, BUNDLE CHOICES A AND B should end with 7 and 8 for each category
		rename (rd_4_a_project rd_4_b_project rd_4_a_technicalassistance rd_4_b_technicalassistance rd_4_a_budget rd_4_b_budget) ///
			   (rd1_project7 rd1_project8  rd1_technicalassistance7 rd1_technicalassistance8 rd1_budget7 rd1_budget8)

	**ROUND 5, BUNDLE CHOICES A AND B should end with 9 and 10 for each category
		rename (rd_5_a_project rd_5_b_project rd_5_a_technicalassistance rd_5_b_technicalassistance rd_5_a_budget rd_5_b_budget) ///
			   (rd1_project9 rd1_project10  rd1_technicalassistance9 rd1_technicalassistance10 rd1_budget9 rd1_budget10)

	**ROUND 6, BUNDLE CHOICES A AND B should end with 11 and 12 for each category
		rename (rd_6_a_project rd_6_b_project rd_6_a_technicalassistance rd_6_b_technicalassistance rd_6_a_budget rd_6_b_budget) ///
			   (rd1_project11 rd1_project12  rd1_technicalassistance11 rd1_technicalassistance12 rd1_budget11 rd1_budget12)
		
		rename (dce1_r1 dce1_r2 dce1_r3 dce1_r4 dce1_r5 dce1_r6) (dce1_r1 dce1_r3 dce1_r5 dce1_r7 dce1_r9 dce1_r11) // 	**here we know which bundle respondent chose

	// Drop respondents who didn't participate in experiment 
	drop if rd1_project1==""

	// Generate new ID and Reshape Data to Long	
	sort responseid
	gen id=_n
	reshape long rd1_project rd1_technicalassistance rd1_budget dce1_r, i(id) j(exp)

	// Clean Rounds and Choice Answers
	**when we reshaped the data we kept the respondents' choice only in the odd bundles (BUNDLE "A" ORIGINALLY - 1, 3, 5 and 7). We also lost the round numbers, and the bundles are no longer 1 and 2, rather a continuos block of 8. Let's now re-create the rounds/question number, and rename bundles to 1 and 2:
	
	**first replace empty cells with the respondent's choice:
	replace dce1_r=dce1_r[_n-1] if dce1_r==""

	**number the rounds:
	gen 	round=1 if exp<3
	replace round=2 if exp==3  | exp==4
	replace round=3 if exp==5  | exp==6
	replace round=4 if exp==7  | exp==8
	replace round=5 if exp==9  | exp==10
	replace round=6 if exp==11 | exp==12

	** Rename bundles into 1 and 2 instead of 1-8 (equivalent to A and B)
	gen 	experiment=1 if exp==1 | exp==3 | exp==5 | exp==7 | exp==9  | exp==11
	replace experiment=2 if exp==4 | exp==6 | exp==8 | exp==2 | exp==10 | exp==12

	** Generate a choice dummy:
	gen 	choice=1 if experiment==1 & dce1_r=="A"
	replace choice=1 if experiment==2 & dce1_r=="B"
	replace choice=0 if choice==. & dce1_r!="" & dce1_r!="C"
	replace choice=0 if dce1_r=="C"
	*drop if dce1_r=="C"
	
	**DATA IS READY FOR REGRESSIONS
	snapshot save, label(dce) // Save the current formatted data. This is easier to then re-do regressions for subgroups (only online, for example)
		
	*destring variables:

	**BUDGET VARIABLES:
	destring rd1_budget, replace ignore(million$)
		
	**TECHNICAL ASSISTANCE:	
	replace  rd1_technicalassistance="0" if rd1_technicalassistance=="None"
	replace  rd1_technicalassistance="1" if rd1_technicalassistance=="1 Full-time Technical Advisor"
	replace  rd1_technicalassistance="2" if rd1_technicalassistance=="2 Full-time Technical Advisors"
	destring rd1_technicalassistance, replace
	
	**GENERATE INTERACTIONS DUMMIES FOR PROJECT:	
	gen project_lite	   = rd1_project=="Foundational literacy"
	gen project_tech	   = rd1_project=="Computers / Technology"
	gen project_assessment = rd1_project=="Learning Assessment"
	gen project_sccons	   = rd1_project=="School construction"
	gen project_tvet	   = rd1_project=="Technical and Vocational Education"		

	lab var rd1_budget 				"Budget (USD million)"
	lab var rd1_technicalassistance "Technical Advisors"
	lab var project_tvet 			"TVET"
	lab var project_assessment 		"Assessment"
	lab var project_lite 			"Foundational Literacy"
	lab var project_sccons 			"School Construction"
	lab var project_tech			"IT"
	
	**gen alternative grouped project category
	gen 	alt_project_other=0
	replace alt_project_other=1 if project_tech==1|project_assessment==1|project_sccons==1 
		
	egen pair=group(id round)	
			
	save "$root/output/dce_projects.dta", replace
	
	// Globals
	global projects  project_tvet project_assessment project_lite project_sccons
	global projects2 project_tvet project_lite 
	global controls3 male lowexp minister subnational i.country_id

****************************************************************************************************	
	
// Table 3: 
	preserve
		drop if dce1_r=="C"
			
	eststo clear
	eststo m1 : qui reghdfe choice rd1_budget rd1_technicalassistance $projects, nocons cluster(pair) a(id) 
		test project_assessment=project_sccons
		test project_assessment=project_lite
		test project_lite=project_sccons
	eststo m2 : qui reghdfe choice rd1_budget rd1_technicalassistance $projects if agency2==1, nocons cluster(pair) a(id) 
		test project_assessment=project_sccons
		test project_assessment=project_lite
		test project_lite=project_sccons
	eststo m3 : qui reghdfe choice rd1_budget rd1_technicalassistance $projects if agency2==0, nocons cluster(pair) a(id) 
		test project_assessment=project_sccons
		test project_assessment=project_lite
		test project_lite=project_sccons
	estfe m1 m2 m3, labels(id "Respondent FE")
	
		//Latex output
		esttab using tables/dce_projects.tex, replace f booktabs nonum se ///
				label stats(N df_a r2,fmt(%9.0gc %9.0gc 3)labels("Obs. (Responses)" "Obs. (Respondents)"  "R\sym{2}")) ///
				starlevels(* 0.1 ** 0.05 *** 0.01) noomitted ///
				indicate(`r(indicate_fe)',  label("Yes" "")) ///
				mtitle("Sample" "Agency" "\& agencies") 

		//Stata output			
		estout m1 m2 m3, replace type style(smcl) ///
				label stats(N r2,fmt(%9.0gc 3 2)labels("N" "R2")) cells(b(star fmt(3)) se(par)) starlevels(* 0.1 ** 0.05 *** 0.01) noomitted ///
				keep  (rd1_budget rd1_technicalassistance $projects) /// indicate("Country FE = *country_id", label("Yes" ""))
				collabel(none) mlabel("All" "All" "TVET" "Other") ///
				indicate(`r(indicate_fe)',  label("Yes" ""))
	restore

****************************************************************************************************	
	
// APPENDIX	
	
* Table B4: Conjoint Experiment Results: Aid Project Preferences (Logit Model) 	
preserve	
	drop if dce1_r=="C"

	eststo clear
	eststo m1 : qui logit choice rd1_budget rd1_technicalassistance $projects, cluster(pair) 
	eststo m5: 	   margins, dydx(rd1_budget rd1_technicalassistance $projects) post

	eststo m2 : qui logit choice rd1_budget rd1_technicalassistance $projects i.id , cluster(pair) 
	eststo m6: 	   margins, dydx(rd1_budget rd1_technicalassistance $projects) post

	eststo m3 : qui logit choice rd1_budget rd1_technicalassistance $projects i.id if agency2==1, cluster(pair) 
	eststo m7: 	   margins, dydx(rd1_budget rd1_technicalassistance $projects) post

	eststo m4 : qui logit choice rd1_budget rd1_technicalassistance $projects i.id if agency2==0, cluster(pair) 
	eststo m8:	   margins, dydx(rd1_budget rd1_technicalassistance $projects) post

	esttab m1 m2 m3 m4 using tables/dce_projects_log.tex, replace f booktabs nonum se ///
				label stats(N r2_p,fmt(%9.0gc %9.0gc 3)labels("Obs. (Responses)" "Pseudo R\sym{2}")) ///
				drop( _cons) ///
				starlevels(* 0.1 ** 0.05 *** 0.01) noomitted ///
				indicate("Resp. FE = *id",  label("Yes" "")) ///
				mtitle("Sample" "Sample" "Agency" "\& agencies") 

	esttab m5 m6 m7 m8 using tables/dce_projects_logmargins.tex, replace booktabs f se ///
				label stats(N,fmt(%9.0gc %9.0gc 3)labels("Obs. (Responses)")) ///
				starlevels(* 0.1 ** 0.05 *** 0.01) noomitted nonumbers ///
				mtitle("Sample" "Sample" "Agency" "\& agencies") b("%12.4f")
restore
	
	
// Table C2: Conjoint Experiment: Aid Project Preferences (Original Sample Only)

preserve
	keep if googlelist==1

	eststo clear
	eststo m1 : qui reghdfe   choice rd1_budget rd1_technicalassistance $projects , nocons cluster(pair) a(id)
		test project_assessment=project_sccons
		test project_assessment=project_lite
		test project_lite=project_sccons
	eststo m2 : qui reghdfe   choice rd1_budget rd1_technicalassistance $projects , nocons cluster(pair) a(id) 
		test project_assessment=project_sccons
		test project_assessment=project_lite
		test project_lite=project_sccons
	eststo m3 : qui reghdfe   choice rd1_budget rd1_technicalassistance $projects  if agency2==1, nocons cluster(pair) a(id) 
		test project_assessment=project_sccons
		test project_assessment=project_lite
		test project_lite=project_sccons
	eststo m4 : qui reghdfe   choice rd1_budget rd1_technicalassistance $projects  if agency2==0, nocons cluster(pair) a(id)
		test project_assessment=project_sccons
		test project_assessment=project_lite
		test project_lite=project_sccons
	estfe m1 m2 m3 m4, labels(id "Respondent FE")

		esttab using tables/dce_projects_robust.tex, replace f booktabs nonum se ///
				label stats(N N_clust r2,fmt(%9.0gc %9.0gc 3)labels("Obs. (Responses)" "Obs. (Respondents)"  "R\sym{2}")) ///
				starlevels(* 0.1 ** 0.05 *** 0.01) noomitted ///
				indicate(`r(indicate_fe)',  label("Yes" "")) ///
				mtitle("Sample" "Sample" "Agency" "\& agencies") 
				
		estout m1 m2 m3 m4, replace type style(smcl) ///
				label stats(N r2,fmt(%9.0gc 3 2)labels("N" "R2")) cells(b(star fmt(3)) se(par)) starlevels(* 0.1 ** 0.05 *** 0.01) noomitted ///
				keep  (rd1_budget rd1_technicalassistance $projects) ///
				collabel(none) mlabel("All" "All" "TVET" "Other") ///
				indicate(`r(indicate_fe)',  label("Yes" ""))
restore
